﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyHocBongSinhVien.DAO;
using QuanLyHocBongSinhVien.Bus;
using System.Data.SqlClient;

namespace QuanLyHocBongSinhVien
{
    public partial class frmKhoa : Form
    {
        Khoa_Bus kb = new Khoa_Bus();
        public frmKhoa()
        {
            InitializeComponent();
        }
        public bool check_id(string id)
        {

            dbConnection db = new dbConnection();
            SqlParameter mabomon = new SqlParameter("@MaKhoa", id);
            DataTable dt = db.ExecuteQuery("SELECT * FROM KHOA WHERE MAKHOA = @MaKhoa", mabomon);
            return dt.Rows.Count > 0;
        }

        private void frmKhoa_Load(object sender, EventArgs e)
        {
            dgvKhoa.DataSource = kb.GetAllKhoa();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (check_id(txtMaKhoa.Text))
            {
                MessageBox.Show("Ban da nhap trung ma");
                return;
            }
            if (txtMaKhoa.Text == "")
            {
                MessageBox.Show("Ban chua nhap thong tin cho ma");
                return;
            }

            if (txtTenKhoa.Text == "")
            {
                MessageBox.Show("Ban chua nhap thong tin cho ten");
                return;
            }
            if (txtTrKhoa.Text == "")
            {
                MessageBox.Show("Ban chua nhap thong tin cho truong khoa");
                return;
            }
            else
            {
                kb.Add(txtMaKhoa.Text,txtTenKhoa.Text,txtTrKhoa.Text,int.Parse(txtSDT.Text),txtemail.Text);
                dgvKhoa.DataSource = kb.GetAllKhoa();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            kb.update(txtMaKhoa.Text, txtTenKhoa.Text, txtTrKhoa.Text, int.Parse(txtSDT.Text), txtemail.Text);
            dgvKhoa.DataSource = kb.GetAllKhoa();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Ban co muon xoa thong tin" + txtMaKhoa.Text + "khong", "thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                kb.Delete(txtMaKhoa.Text);
                dgvKhoa.DataSource = kb.GetAllKhoa();
            }
        }


        private void btnDong_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            txtMaKhoa.Clear();
            txtTenKhoa.Clear();
            txtTrKhoa.Clear();
            txtSDT.Clear();
            txtemail.Clear();
        }
    }
}
